package controllers;

import java.awt.event.ActionEvent;
import java.util.Vector;

import dialogs.DeleteTupleDialog;
import dialogs.InsertTupleDialog;

import models.ReturnTransactionModel;

import system.View;

public class ReturnTransactionController extends AbstractMenuItemController {
	private ReturnTransactionModel return_transaction = new ReturnTransactionModel();

	public ReturnTransactionController(View v) {
		super(v);
		abstractModel = return_transaction;
		return_transaction.addExceptionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();

		if (actionCommand.equals("Delete Return"))
			showDialogWindow(new DeleteTupleDialog(view, actionCommand, "Key",
					new String[] { "Return Transaction ID: " }, this));
		else if (actionCommand.equals("Insert Return"))
			showDialogWindow(new InsertTupleDialog(view, actionCommand,
					"Return", new String[] { "Receipt ID Number: ", 
					"Name: "  // look up what this name refers to
					}, this));
	}

	public int validateInsert(Vector<String> s) {
		if (s.size() != 2)
			return VALIDATIONERROR;

		try {
			int receiptId;
			String name;

			if (s.get(0).length() != 0)
				receiptId = Integer.valueOf(s.get(0).trim());
			else return VALIDATIONERROR;
			if (s.get(1).length() != 0)
				name = s.get(1).trim();
			else return VALIDATIONERROR;

			view.updateStatusBar("Inserting return transaction...");

			if (return_transaction.insert(receiptId, name)) return operationSuccess();
			else return operationFailure();
		} catch (NumberFormatException ex) {
			return VALIDATIONERROR;
		}
	}

	@Override
	public int validateDelete(Vector<String> s) {
		if (s.size() != 1)
			return VALIDATIONERROR;

		try {
			Integer retid;

			if (s.get(0).trim().length() != 0)
				retid = Integer.valueOf(s.get(0).trim());
			else return VALIDATIONERROR;

			view.updateStatusBar("Deleting Return Transaction...");

			if (return_transaction.delete(retid)) return operationSuccess();
			else return operationFailure();
		} catch (NumberFormatException ex) {
			return VALIDATIONERROR;
		}
	}
}